﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;

using Dingding.Helper;


namespace DingApi.Controllers
{
    public class UsersController : ApiController
    {
        User[] users = new User[] 
        { 
             new User { Id=1, UserCode = "a1", UserName = "xxx" }, 
             new User { Id=2, UserCode = "a2", UserName = "yyy" }
         };



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<User> GetAllUsers()
        {

            User[] userid = new User[] 
            { 
                 new User { Id = 11,UserCode = "getx", UserName = "xxx" }, 
                 new User { Id = 21, UserCode = "gety", UserName = "yyy" }
             };

           
            return userid;
        }

        /// <summary>
        /// 单个参数获取，也可多个，或者用对象接收；
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public User GetUserById(string id)
        {

            var Obj = DingDingHelper.GetAccessToken();

            string s_token = Obj.access_token;
            var Obj2 = DingDingHelper.GetUserInfoV2(s_token, id.ToString());
            var userid = new User { UserCode = id, UserName = Obj2.user_info.ToString() };
            return userid;
        }



       

        public IEnumerable<User> PostAllUsers()
        {
            User[] userid = new User[] 
            { 
                 new User { UserCode = "px", UserName = "xxx" }, 
                 new User { UserCode = "py", UserName = "yyy" }
             };


            return userid;
        }

        public User PostUserById(string code)
        {


            var Obj = DingDingHelper.GetAccessTokenV2();
            string s_token = Obj.access_token;

            var Obj2 = DingDingHelper.GetUserInfoV2(s_token, code);

            var userid = new User { UserCode = Obj2.user_info.ToString(), UserName = "zz" };



            //var User = users.FirstOrDefault((p) => p.UserCode == code);
            //if (User == null)
            //{
            //    throw new HttpResponseException(HttpStatusCode.NotFound);               
            //}
            return userid;
        }

       
    }
}